package com.zues.java_test.juc_test;

import java.net.InetAddress;

public class TPTInterrupt {

    private Thread thread;

    public void start(){
        new Thread(() -> {
            while (true){
                Thread currentThread = Thread.currentThread();
                if(currentThread.isInterrupted()){
                    System.out.println("关闭");
//                    break;
                }

                try {
                    Thread.sleep(3000);
                    System.out.println("将结果保存");
                } catch (InterruptedException e) {
                    currentThread.interrupt();
                }
                //执行监控操作 更新设备连接状态

            }
        },"监控线程").start();
    }


    /**
     * 打断当前线程
     */
    public void stop(){
        Thread.interrupted();
    }


    /**
     * 判断IP是否连接
     */
    public static boolean isConnect(String ip) {
        //测试IP通信
        try {
            InetAddress address = InetAddress.getByName(ip);
            return address.isReachable(3000); // 3000毫秒超时

        } catch (Exception e) {
            System.out.println("Error: " + e.getMessage());
            return false;
        }
    }
}
